package com.ebelter.scaleblesdk.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;


/**
 * @author 作 者：pan
 * @filename 文件名：EncryptUtil.java
 * @description 描 述：加密工具
 * @date 时 间：2017-8-31
 * @Copyright 版 权：倍泰源代码，版权归深圳市倍泰健康测量分析技术有限公司所有。
 */
public class EncryptUtil {

    private static String ivParameter;
    private static String charSet;


    // 加密
    public static String encrypt(String sSrc, String encodingFormat, String sKey,
                                 String ivParameter) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));
        return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。
    }

    // 解密
    public static String decrypt(String sSrc, String encodingFormat, String sKey,
                                 String ivParameter) throws Exception {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, encodingFormat);
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

    public static String decrypt(String enString, String s) {
        String deString = null;
        try {
            deString = EncryptUtil.decrypt(enString, charSet, s, ivParameter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deString;
    }

    public static String encrypt(String content, String s) {
        String deString = null;
        try {
            deString = EncryptUtil.encrypt(content, charSet, s, ivParameter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deString;
    }

    public static void init() {
        ivParameter = "asdvjer#522d4fef";
        charSet = "utf-8";
    }

    public static String xor(byte[] ints) {
        StringBuffer sb = new StringBuffer();
        for (int i : ints) {
            i = i ^ 0x11;
            sb.append((char) i);
        }
        return sb.toString();
    }

    public static byte[] xor(String s) {
        byte[] sb = new byte[s.length()];
        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            char ch = chars[i];
            int a = ch;
            a = a ^ 0x11;
            sb[i] = (byte) a;
        }
        return sb;
    }

}
